package com.kstech.test.wqc.fragment;

import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.kstech.test.wqc.Globals;
import com.kstech.test.wqc.R;

import java.util.ArrayList;

/**
 * 对于检测数据生成展示的模块 header 和 content
 * Created by lenovo on 2016/11/2.
 */

public class RecyclerDataFragment extends Fragment {

    private ArrayList<String> headlist = new ArrayList<>();
    private ArrayList<RecyclerView> viewLists = new ArrayList<>();
    private RecyclerView head;
    private ListView  lv;
    private ListAdapter listAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_inflate, null);
        init(view);
        Log.i("Fragment","onCreateView");
        return view;
    }

    private void init(View view) {
        for (int i = 0; i < Globals.values.size(); i++) {
            headlist.add(Globals.values.get(i).getItem());
        }
        head = (RecyclerView) view.findViewById(R.id.head);
        lv = (ListView) view.findViewById(R.id.lv_content);
        listAdapter = new ListAdapter();
        head.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        head.setAdapter(new MyAdapter(headlist));
        lv.setAdapter(listAdapter);
    }
    public void update(){
        RecyclerView rl = new RecyclerView(getActivity());
        rl.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
        rl.setAdapter(new MyAdapter(Globals.content));
        viewLists.add(rl);
        listAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPause() {
        super.onPause();
        Log.i("Fragment","OnPause");
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i("Fragment","onResume");
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        Log.i("Fragment","onDestroyView");
    }

    class ListAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return viewLists.size();
        }

        @Override
        public Object getItem(int i) {
            return viewLists.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            return viewLists.get(i);
        }
    }
}
